<?php
header("Content-Type: application/json; charset=UTF-8");
require_once 'db.php';

$id = isset($_GET['id']) ? intval($_GET['id']) : 0;

if ($id <= 0) {
    echo json_encode([
        'status' => 'error',
        'message' => '无效的公告ID'
    ]);
    exit;
}

// 更新浏览次数
$update_sql = "UPDATE notices SET views = views + 1 WHERE id = ?";
$update_stmt = $conn->prepare($update_sql);
$update_stmt->bind_param("i", $id);
$update_stmt->execute();
$update_stmt->close();

// 获取公告详情
$sql = "SELECT * FROM notices WHERE id = ? AND status = 1";
$stmt = $conn->prepare($sql);
$stmt->bind_param("i", $id);
$stmt->execute();
$result = $stmt->get_result();

if ($result->num_rows > 0) {
    $notice = $result->fetch_assoc();
    // 格式化时间
    $notice['created_at'] = date('Y-m-d H:i', strtotime($notice['created_at']));
    echo json_encode([
        'status' => 'success',
        'notice' => $notice
    ]);
} else {
    echo json_encode([
        'status' => 'error',
        'message' => '公告不存在'
    ]);
}

$stmt->close();
$conn->close();
?> 